package paskal.ui.components;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.Map;

import javax.swing.JComponent;
import javax.swing.JScrollBar;
import javax.swing.plaf.basic.BasicScrollBarUI;

import paskal.ui.Style;

public class ScrollBar extends JScrollBar implements Component {
	private static final long serialVersionUID = -8698320103760232913L;
	private Thumb thumb = null;
	private BufferedImage trackImage = null;
	private Color trackPColor = null;
	private int trackSize = 3;

	public ScrollBar() {
		setOpaque(false);
		setBackground(new Color(0, 0, 0, 0));
		setUnitIncrement(23);
		setCursor(new Cursor(Cursor.HAND_CURSOR));
	}

	@Override
	protected void paintComponent(Graphics g) {
		g.setColor(getBackground());
		g.fillRect(0, 0, getWidth(), getHeight());
		super.paintComponent(g);
	}

	@Override
	public void stylize(String selector) {
		Style.getInstance().styleThisShit(selector, this);
	}

	public void setProperty(String key, Object value) {
		switch (key) {
		case "bounds": {
			setBounds((Rectangle) value);
			break;
		}
		case "track": {
			@SuppressWarnings("unchecked")
			Map<String, Object> track = (Map<String, Object>) value;

			@SuppressWarnings("unchecked")
			Map<String, Object> background = (Map<String, Object>) track
					.get("background");

			if (track.get("size") != null) {
				trackSize = (int) track.get("size");
			}

			String path = (String) background.get("image");
			if (path != null) {
				Rectangle r = (Rectangle) background.get("position");
				if (r != null) {
					this.trackImage = Style.getInstance().getImage(
							(String) background.get("image"), r);
				} else {
					this.trackImage = Style.getInstance().getImage(
							(String) background.get("image"));
				}
			}

			Object o = background.get("color");
			if (o != null) {
				trackPColor = new Color(Integer.parseInt(o.toString()
						.substring(1), 16));
			}

			break;
		}
		case "thumb": {
			@SuppressWarnings("unchecked")
			Map<String, Object> thumb = (Map<String, Object>) value;
			this.thumb = new Thumb();
			Style.getInstance().styleThisShit(thumb, this.thumb);
			addMouseListener(this.thumb);
			break;
		}
		default:
			break;
		}
		setUI(new BasicScrollBarUI() {
			@Override
			protected void installComponents() {
				super.installComponents();
				remove(this.decrButton);
				remove(this.incrButton);
			}

			@Override
			protected void paintDecreaseHighlight(Graphics g) {
			}

			@Override
			protected void paintIncreaseHighlight(Graphics g) {
			}

			@Override
			protected void paintThumb(Graphics g, JComponent c,
					Rectangle thumbBounds) {
				BufferedImage[] img = thumb.getSplitedCurrentIcon();
				Rectangle bounds = getThumbBounds();
				int x = bounds.x + (bounds.width - img[0].getWidth()) / 2;
				g.drawImage(img[0], x, bounds.y - decrButton.getHeight(), null);

				g.drawImage(
						img[1],
						x,
						bounds.y + img[0].getHeight() - decrButton.getHeight(),
						img[1].getWidth(),
						bounds.height + incrButton.getHeight() * 2
								- img[2].getHeight() - img[0].getHeight(), null);

				g.drawImage(img[2], x,
						bounds.y + bounds.height + incrButton.getHeight()
								- img[2].getHeight(), null);
			}

			@Override
			protected void paintTrack(Graphics g, JComponent c,
					Rectangle trackBounds) {
				if (trackPColor != null) {
					g.setColor(trackPColor);
					g.fillRect(trackRect.x + (trackRect.width - trackSize) / 2,
							trackRect.y - decrButton.getHeight(), trackSize,
							trackRect.height + incrButton.getHeight() * 2);
				}
				if (trackImage != null) {
					g.drawImage(trackImage, trackRect.x
							+ (trackRect.width - trackSize) / 2, trackRect.y
							- decrButton.getHeight(), trackSize,
							trackRect.height + incrButton.getHeight() * 2, null);
				}
			}
		});
	}
}